﻿using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using LINQtoNHibernateMVC.Infrastructure.DataAccess.Impl;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace LINQtoNHibernateMVC.Infrastructure.DataAccess {
	public class NHibernateHelper {
		public ISessionFactory BuildSessionFactory() {
			return Fluently.Configure()
			   .Database(MsSqlConfiguration
				  .MsSql2005
				  .ConnectionString(c => c.FromConnectionStringWithKey("DBConnectionString"))
				  .ShowSql())
			   .Mappings(mapping => mapping.FluentMappings.AddFromAssemblyOf<Repository>())
			   //.ExposeConfiguration(BuildSchema)
			   .BuildSessionFactory();
		}

		private static void BuildSchema(Configuration config) {
			// Create database schema (creates your database, location based off of the web.config IW3 connection string setting)
			new SchemaExport(config)
			  .Create(false, true);

			//// Update your database schema (updates existing database)
			//new SchemaUpdate(config)
			//	.Execute(false, true);
		}
	}
}
